import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';

interface obj {
  [key: string]: Array<RouteRecordRaw>;
}

const moduleFiles = require.context('./routes', true, /\.ts$/);

const modules = moduleFiles.keys().reduce((modules: obj, modulePath) => {
  const moduleName: string = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1');
  const value = moduleFiles(modulePath);

  modules[moduleName] = value.default;
  return modules;
}, {});

const moduleValues: Array<Array<RouteRecordRaw>> = Object.values(modules);

const routes: Array<RouteRecordRaw> = [];

moduleValues.forEach((value: Array<RouteRecordRaw>) => {
  value.forEach((value: RouteRecordRaw) => {
    routes.push(value);
  });
});

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

export default router;
